package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.pojo.param.BrandInsertParam;
import cn.tedu.csmall.product.pojo.param.BrandUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.BrandListVO;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/brand")
@Api(tags = "08.品牌管理模块")
@Slf4j
@Validated
public class BrandController {

    @Autowired
    BrandService service;
    @PostMapping("/insert")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult insert(@Valid BrandInsertParam brandInsertParam){
            service.insert(brandInsertParam);
            return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除品牌失败，请提交合法的ID值")
                           @RequestParam Long brandId) {
        service.deleteById(brandId);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改品牌")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@Valid BrandUpdateInfoParam brandUpdateInfoParam){
        service.update(brandUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("查看品牌")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false,paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min=1,message = "请输入合法的页码") Integer pageNum,
                           String queryType){
        if(pageNum==null){
            pageNum=1;
        }
        PageData<BrandListVO> pageData;
        if("all".equals(queryType)){
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        }else {
            pageData = service.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

    @GetMapping("/getStandard")
    @ApiOperation("根据ID查询品牌详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", value = "品牌ID", required = true, dataType = "long")
    })
    public JsonResult getStandard(@Range(min = 1,message = "根据ID查看品牌详情失败，请提交合法的ID值")
                                  @RequestParam Long brandId) throws Exception {
        return JsonResult.ok(service.getStandardById(brandId));
    }
}
